﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BBMS.DLL.DataContract
{
    public class Quay : BBMS.DLL.Base.ObjectBase
    {
        public string Ma;
        public string Ten;
        public string TenTA;
        public string MaNganhHang;

        public Quay() 
        {
        }

        public Quay(string ma, string ten, string tenTA, string maNganhHang)
        {
            this.Ma = ma;
            this.Ten = ten;
            this.TenTA= tenTA;
            this.MaNganhHang = maNganhHang;
        }

        public override object Key
        {
            get
            {
                return this.Ma;
            }
        }
        public override string Name
        {
            get
            {
                return this.Ten;
            }
        }
        public override string  TableName
        {
	        get 
	        {
                return "Counter";
	        }
        }
        
        public override string[] KeyFields
        {
            get
            {
                return new string[] { "Ma" };
            }
        }

        public override string[]  IDFields
        {
            get
            {
                return new string[] { "Ma" };
            }
        }
        public override bool CanDelete(MySql.Data.MySqlClient.MySqlCommand cmd, System.Data.DataRow row)
        {
            bool ret = false;
            try
            {
                //return base.CanDelete(cmd, row);
                cmd.CommandText = "select count(MaQuay) from bill where MaQuay = @MaQuay";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@MaQuay", this.Ma);
                long count = (long)cmd.ExecuteScalar();
                if (count == 0)
                {
                    ret = true;
                }
                cmd.Parameters.Clear();
            }
            catch { }
            return ret;
        }

        public override System.Data.DataTable GetObjectDataTable()
        {
            System.Data.DataTable dt = new System.Data.DataTable(this.TableName);
            dt.Columns.Add("Ma", typeof(String));
            dt.Columns.Add("Ten", typeof(String));
            dt.Columns.Add("TenTiengAnh", typeof(String));
            dt.Columns.Add("MaNganhHang", typeof(String));
            return dt;
        }
        public override System.Data.DataRow ObjectToDataRow()
        {
            System.Data.DataRow row = this.GetObjectDataTable().NewRow();
            row["Ma"] = this.Ma;
            row["Ten"] = this.Ten;
            row["TenTiengAnh"] = this.TenTA;
            row["MaNganhHang"] = this.MaNganhHang;
            return row;
        }

        public override BBMS.DLL.Base.ObjectBase DataRowToObject(System.Data.DataRow row)
        {
            Quay obj = null;
            try
            {
                obj = new Quay(row["Ma"].ToString(), row["Ten"].ToString(),
                    row["TenTiengAnh"].ToString(), row["MaNganhHang"].ToString());
            }
            catch { }
            return obj;
        }

        public override BBMS.DLL.Base.ObjectBase DataReaderToObject(MySql.Data.MySqlClient.MySqlDataReader rd)
        {
            Quay obj = null;
            try
            {
                obj = new Quay(rd["Ma"].ToString(), rd["Ten"].ToString(), 
                     rd["TenTiengAnh"].ToString(), rd["MaNganhHang"].ToString());
            }
            catch { }
            return obj;
        }

        public override string InsertNew()
        {
            string str = base.InsertNew();
            if (str == "") ObjectLists.QuayList.Add(this);
            return str;
        }
        public override string Delete()
        {
            string str = base.Delete();
            if (str == "") ObjectLists.QuayList.Remove(this);
            return str;
        }
        public override string Save()
        {
            string str = base.Save();
            if (str == "") ObjectLists.QuayList.Update(this);
            return str;
        }
    }
}
